<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class MasterProfile extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model("masterprofilealumni_model", "mpa");
    }

    function index() {
        $perpage = 30;
        $curpage = $this->uri->segment(3);
        $data["ddFilter"] = $this->input->post("ddFilter");
        $data["txtSearch"] = $this->input->post("txtSearch");
        $like = "";
        if ($data["txtSearch"] != "")
            $like = array($data["ddFilter"] => $data["txtSearch"]);
        $data["alumni"] = $this->mpa->getAll($curpage, $perpage, array('isdelete' => 0), @$like, "mpa_graduate_year, mpa_fullname");
        $this->popuppaging(base_url()."masterprofile/index", $this->mpa->getAllCount(array('isdelete' => 0), @$like), $perpage);
        $this->myview(@$data, "masterprofile/index");
    }
    
    function edit() {
        $this->myview("", "masterprofile/edit");
    }
    
    function delete() {
        $this->mpa->delete($this->uri->segment(3));
        $this->session->set_flashdata("message", "Master profile berhasil dihapus");
        $this->session->set_flashdata("redir", base_url() . "masterprofile");
        redirect(base_url() . "alert/success");
    }
    
    function upload() {
        $this->myview("", "masterprofile/upload");
    }
    
    function do_upload() {
        require_once(APPPATH.'/libraries/excel_reader.php');
            
        $config['upload_path'] = APPPATH.'/uploads/';
        $config['allowed_types'] = 'xls';
        $config['max_size']	= '1000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload()){
            $data['upload'] = array('error' => $this->upload->display_errors());
            $this->myview(@$data, "masterprofile/upload");
        } else {   
            $data['upload'] = $this->upload->data();
            $excel = new Spreadsheet_Excel_Reader($data['upload']['full_path']);
            $row = $excel->rowcount(0);
            $data['upl_status'] = "1";
            $data['result'] = "";
            $alumni = array();
            
            $empty_row = 0;
            $valid_row = 0;
            $invalid_row = 0;
            $total_row = 0;
            
            for ($i=2; $i<=$row; $i++){
                $total_row = $total_row +1;
                if ($excel->val($i, 1) =='' && $excel->val($i, 2)  == '' && $excel->val($i, 3) == '' && $excel->val($i, 4) == '' && $excel->val($i, 5) == '' && $excel->val($i, 6) == '' && $excel->val($i, 7) == '' && $excel->val($i, 8) == '' && $excel->val($i, 9) == '' && $excel->val($i, 10) == '') {
                    $empty_row = $empty_row + 1;
                } else {
                    $rec = new stdClass();
                    $rec->nis = $excel->val($i, 1);
                    $rec->fullname = $excel->val($i, 2);
                    $rec->gender = $excel->val($i, 3);
                    $rec->birth_date = $excel->val($i, 4);
                    $rec->graduate_year = $excel->val($i, 5);
                    $rec->fathername = $excel->val($i, 6);
                    $rec->mothername = $excel->val($i, 7);
                    $rec->home_address = $excel->val($i, 8);
                    $rec->home_city = $excel->val($i, 9);
                    $rec->home_phone = $nis = $excel->val($i, 10);
                    $rec->error =  array();
                    $rec->row =  $i-1;
                
                    if ($rec->nis == '') array_push($rec->error, 'NIS tidak valid');
                    if ($rec->fullname == '') array_push($rec->error, 'FULLNAME tidak valid');
                    if ($rec->gender != 'L' && $rec->gender != 'P') array_push($rec->error, 'GENDER tidak valid');
                    if ($rec->birth_date == '') array_push($rec->error,'BIRTHDATE tidak valid');
                    if ($rec->graduate_year == '') array_push($rec->error, 'GRADUATE YEAR tidak valid');
                    
                    if (!empty($rec->error)) {
                        log_message('debug',print_r($rec->error, true));
                        $data['upl_status'] = "0";
                        $invalid_row = $invalid_row + 1;
                    } else {
                        $valid_row = $valid_row + 1;
                    }
                    array_push($alumni, $rec);
                }
            }
            
            $data['empty_row'] = $empty_row;
            $data['valid_row'] = $valid_row;
            $data['invalid_row'] = $invalid_row;
            $data['total_row'] = $total_row;
            
            if ($data['upl_status'] == "1") {
                for ($i=0; $i<count($alumni); $i++){
                    $this->mpa->insert(
                            $alumni[$i]->nis, 
                            $alumni[$i]->fullname, 
                            $alumni[$i]->birth_date, 
                            $alumni[$i]->gender, 
                            $alumni[$i]->graduate_year, 
                            $alumni[$i]->fathername, 
                            $alumni[$i]->mothername, 
                            $alumni[$i]->home_address, 
                            $alumni[$i]->home_city, 
                            $alumni[$i]->home_phone, 
                            '0', 
                            $this->session->userdata('ua_username'));
                }
                $data['upl_message'] = "berhasil";    
            } else {
                $data['alumni'] = $alumni;
                $data['upl_message'] = "gagal";
            }
            log_message('debug',print_r($data, true));
            $this->myview(@$data, "masterprofile/result");
        }
    }
    
    function template() {
        $this->load->helper('download');
        $data = file_get_contents(APPPATH."/uploads/masterprofile_tpl.xls");
        $name = 'masterprofile_tpl.xls';

        force_download($name, $data);
    }
}
?>
